#include "StdAfx.h"
#include "SubStructure.h"
#include "Enemy.h"
#include "JacoEnemy.h"
#include "EnemyFactory.h"

EnemyFactory::EnemyFactory()
{
	reader = jsonreader::Instance();
	if(D3DXCreateTextureFromFileEx(pd3dDevice, _T("enemy2.png"), 640, 480, 1, NULL, D3DFMT_UNKNOWN, D3DPOOL_MANAGED,
		D3DX_FILTER_NONE, D3DX_FILTER_NONE, 0x00000000, NULL, NULL, &mobtex1))
		MessageBox(NULL, _T("enemy2.png is not loaded!!!"), _T("ERROR"), MB_OK);
}

EnemyFactory* EnemyFactory::Instance()
{
	if(_p_this == NULL)
	{
		_p_this = new EnemyFactory();
	}
	return _p_this;
}

EnemyFactory::~EnemyFactory()
{
	mobtex1->Release();
}

Enemy* EnemyFactory::MakeEnemy(Event_struct* Enemyinfo)
{
	JacoEnemy* nE;
	int AniId = 0;
	RECT temprect;
	BoundBoxVec tempvec;
	BoundingBox* tempbound;

	if(Enemyinfo->flag < 8)
		AniId = 1001;
	else if(Enemyinfo->flag & 8)
		AniId = 1002;
	else if(Enemyinfo->flag & 16)
		AniId = 1003;
	else if(Enemyinfo->flag & 32)
		AniId = 1004;

	nE = new JacoEnemy();
	if(AniId == 1001)
	{
		temprect.top = 0;
		temprect.bottom = 32;
		temprect.left = 32* Enemyinfo->flag;
		temprect.right = 32 + 32 * Enemyinfo->flag;
		nE->Set_ani(reader->getAni(1001, mobtex1, temprect));
		nE->Set_move(reader->getMove(Enemyinfo->moveID));
		tempbound = new BoundingBox();
		
		tempbound->LT.x = 2.0f;
		tempbound->LT.y = 2.0f;
		tempbound->RB.x = 30.0f;
		tempbound->RB.y = 30.0f;
		tempvec.push_back(tempbound);
		nE->Set_Bound(tempvec);
	}
	else if(AniId == 1002)
	{
		temprect.top = 256* Enemyinfo->flag;
		temprect.bottom = 32 + 256* Enemyinfo->flag;
		temprect.left = 0;
		temprect.right = 32;

		nE->Set_ani(reader->getAni(1002, mobtex2, temprect));
		nE->Set_move(reader->getMove(Enemyinfo->moveID));
		tempbound = new BoundingBox();
		
		tempbound->LT.x = 2.0f;
		tempbound->LT.y = 2.0f;
		tempbound->RB.x = 30.0f;
		tempbound->RB.y = 30.0f;
		tempvec.push_back(tempbound);
		nE->Set_Bound(tempvec);
	}
	else if(AniId == 1003)
	{
	}
	
	nE->Set_item(Enemyinfo->sub1, Enemyinfo->sub2);
	nE->Set_life(Enemyinfo->kind);
	nE->Set_pattern(reader->getBulletEventL(Enemyinfo->makeevent));
	nE->Set_Pos(Enemyinfo->makePos);

	delete Enemyinfo;

	return nE;
}